package com.itcast.tanhuaserver.controller;

import com.itcast.tanhua.dubbo.server.domain.Movements;
import com.itcast.tanhua.dubbo.server.vo.PageInfo;
import com.itcast.tanhuaserver.service.MovementsService;
import com.itcast.tanhuaserver.service.QuanziMQService;
import com.itcast.tanhuaserver.vo.PageResult;
import com.itcast.tanhuaserver.vo.VisitorsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * @PachageName: com.itcast.tanhuaserver.controller
 * @ClassName:  MovementsController
 * @Description: 发布动态controller
 * @Author: Hushi
 * @DateTime: 2021/2/1716:10
 * @Version 1.0
 */
@RestController
@RequestMapping("movements")
@Slf4j
public class MovementsController {
    @Autowired
    private MovementsService movementsService;

    @Autowired
    private QuanziMQService quanziMQService;

    @PostMapping
    public ResponseEntity<Void> savePublish(@RequestParam("textContent") String textContent,
                                            @RequestParam("location") String location,
                                            @RequestParam("longitude") String longitude,
                                            @RequestParam("latitude") String latitude,
                                            @RequestParam("imageContent")MultipartFile[] image,
                                            @RequestHeader("Authorization") String token){
        try {
            String publish = this.movementsService.savePublish(textContent,location,longitude,latitude,image,token);
            if (publish != null){
                quanziMQService.publishMsg(publish);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("保存发布动态错误 ~ token : " + token,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询好友动态
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryPublishList(@RequestParam("page") Integer page,@RequestParam("pagesize") Integer pageSize){
        try {
            PageResult pageResult = this.movementsService.queryPublishList(page,pageSize,false);
            if (pageResult != null){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询好友动态错误 ~ ",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询推荐动态
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("recommend")
    public ResponseEntity<PageResult> queryRecommendPublishList(@RequestParam("page") Integer page,@RequestParam("pagesize") Integer pageSize){
        try {
            PageResult pageResult = this.movementsService.queryPublishList(page,pageSize,true);
            if (pageResult != null){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询推荐动态错误 ~ ",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId){
        try {
            Long count = this.movementsService.likeComment(publishId);
            if (count != null){
                this.quanziMQService.likePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            log.error("点赞功能错误 ~  publishId=" + publishId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId){

        try {
            Long count = this.movementsService.disLikeComment(publishId);
            if (count != null){
                this.quanziMQService.disLikePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            log.error("取消点赞错误 ~  publishId=" + publishId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id") String publishId){
        try {
            Long count = this.movementsService.loveComment(publishId);
            if (count != null){
                this.quanziMQService.lovePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            log.error("喜欢功能错误 ~  publishId=" + publishId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> unloveComment(@PathVariable("id") String publishId){

        try {
            Long count = this.movementsService.unloveComment(publishId);
            if (count != null){
                this.quanziMQService.disLovePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            log.error("取消喜欢错误 ~  publishId=" + publishId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<Movements> queryMovementsById(@PathVariable("id") String publishId){
        try {
            Movements movements =  this.movementsService.queryMovementsById(publishId);
            if (movements != null){
                this.quanziMQService.queryPublishMsg(publishId);
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            log.error("查询单条动态错误~ publishId : " + publishId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 谁看过我
     *
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList(){
        try {
            List<VisitorsVo> visitorsVoList = this.movementsService.queryVisitorsList();
            return ResponseEntity.ok(visitorsVoList);
        } catch (Exception e) {
            log.error("谁看过我~ ",e);
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 自己的所有动态
     *
     * @return
     */
    @GetMapping("all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize,@RequestParam(value = "userId") Long userId){
        try {
            PageResult pageResult = this.movementsService.queryAlbumList(page,pageSize,userId);
            if (null != pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查看相册表错误~ userId = " + userId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
